/*******************************************************************************
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package tr.edu.metu.is.mega.server.service;

import java.util.List;

import javax.servlet.http.HttpSession;

import tr.edu.metu.is.mega.client.service.GwtSubmissionViewService;
import tr.edu.metu.is.mega.common.dto.ReviewerDTO;
import tr.edu.metu.is.mega.common.dto.SubmissionDTO;
import tr.edu.metu.is.mega.common.dto.SubmissionRevisionDTO;
import tr.edu.metu.is.mega.common.dto.SubmissionViewDTO;
import tr.edu.metu.is.mega.common.dto.UserDTO;
import tr.edu.metu.is.mega.common.dto.UserRole;
import tr.edu.metu.is.mega.server.business.SubmissionRevisionManager;
import tr.edu.metu.is.mega.server.business.SubmissionViewManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * Schedule view implementation.
 */
public class GwtSubmissionViewServiceImpl extends RemoteServiceServlet implements GwtSubmissionViewService {

    private static final long serialVersionUID = -6804239722876171905L;

    private final SubmissionViewManager manager;

    private final SubmissionRevisionManager revisionManager;

    /**
     * Default constructor.
     */
    public GwtSubmissionViewServiceImpl() {
        manager = new SubmissionViewManager();
        revisionManager = new SubmissionRevisionManager();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void uploadSubmission(SubmissionDTO view) throws Exception {
        HttpSession session = getThreadLocalRequest().getSession(true);
        view.setSystemuser((UserDTO) session.getAttribute("mega_user"));
        manager.uploadSubmission(view);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void assignReviewer(ReviewerDTO view) {
        manager.assignReviewer(view);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<SubmissionViewDTO> getSubmissions(UserRole role, int courseid, int teamid) {
        return manager.getSubmissions(role, courseid, teamid);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<SubmissionRevisionDTO> getRevisions(int activityid, int teamid) {
        return revisionManager.getRevisions(activityid, teamid);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String downloadAll(UserRole role, int courseid, int teamid) {
        return manager.downloadAll(role, courseid, teamid);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<SubmissionDTO> querySubmissionsToReview(int reviewerTeam) {
        return manager.querySubmissionsToReview(reviewerTeam);
    }
}
